#!/usr/bin/env python3
"""
快速修复测试
"""

import requests
import sqlite3

def test_current_system():
    """测试当前系统状态"""
    print("🔍 测试当前系统状态")
    print("=" * 40)
    
    # 获取项目ID
    try:
        conn = sqlite3.connect('data/research.db')
        cursor = conn.cursor()
        cursor.execute("SELECT id, title FROM projects LIMIT 1")
        project = cursor.fetchone()
        conn.close()
        
        if project:
            project_id = project[0]
            project_title = project[1]
            print(f"📝 测试项目: {project_title}")
            print(f"🆔 项目ID: {project_id}")
        else:
            print("❌ 没有项目数据")
            return
            
    except Exception as e:
        print(f"❌ 数据库错误: {e}")
        return
    
    # 测试各个端点
    base_url = "http://localhost:8080"
    
    print(f"\n🧪 测试端点:")
    
    # 测试项目详情页面
    try:
        response = requests.get(f"{base_url}/project/{project_id}", timeout=3)
        print(f"📄 项目详情页面: {response.status_code}")
        if response.status_code == 404:
            print("   ❌ 404错误 - 路由不存在")
        elif response.status_code == 200:
            print("   ✅ 页面正常")
    except Exception as e:
        print(f"   ❌ 请求失败: {e}")
    
    # 测试项目详情API
    try:
        response = requests.get(f"{base_url}/api/projects/{project_id}", timeout=3)
        print(f"🔌 项目详情API: {response.status_code}")
        if response.status_code == 404:
            print("   ❌ 404错误 - API路由不存在")
        elif response.status_code == 200:
            print("   ✅ API正常")
    except Exception as e:
        print(f"   ❌ 请求失败: {e}")
    
    # 测试项目列表API
    try:
        response = requests.get(f"{base_url}/api/projects", timeout=3)
        print(f"📋 项目列表API: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                print(f"   ✅ API正常 - {len(data.get('projects', []))} 个项目")
            else:
                print(f"   ❌ API错误: {data.get('error')}")
    except Exception as e:
        print(f"   ❌ 请求失败: {e}")
    
    print(f"\n💡 解决方案:")
    print("1. 当前系统缺少项目详情路由")
    print("2. 需要重启系统应用修复")
    print("3. 修复后的start.py已包含所需路由")
    print("\n🔄 重启命令: python start.py")

if __name__ == '__main__':
    test_current_system()